package com.yyfax.pay.depository.controller.api;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.yyfax.framework.common.OpResponse;
import com.yyfax.pay.common.annotation.HeaderSign;
import com.yyfax.pay.common.constants.PayConstants;
import com.yyfax.pay.common.enums.FuiouBusiTypeEnum;
import com.yyfax.pay.common.enums.PayTypeEnum;
import com.yyfax.pay.common.exception.PayExceptionCode;
import com.yyfax.pay.depository.domain.FuiouFileDownload;
import com.yyfax.pay.depository.domain.FuiouFileUpload;
import com.yyfax.pay.depository.service.inner.FuiouDepositFileInnerService;
import com.yyfax.pay.depository.vo.FuiouDownloadFileVO;
import com.yyfax.pay.depository.vo.FuiouFileQueryVO;
import com.yyfax.pay.depository.vo.FuiouUploadFileVO;
import com.yyfax.pay.vo.depository.DownloadFileReq;
import com.yyfax.pay.vo.depository.FileQueryReq;
import com.yyfax.pay.vo.depository.UploadFileReq;

/**
 * 文件入口
 * 
 * @author buyi
 * @date 2017-09-05 17:34:34
 * @since v2.0.0
 */
@RestController
public class FileApiController {

	@Resource
	private PayConstants payConstants;

	@Resource
	private FuiouDepositFileInnerService fuiouDepositFileInnerService;

	/**
	 * 上传文件
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:40:45
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/upload")
	public OpResponse tradeUpload(@RequestBody @Valid UploadFileReq params) {
		validBatchMaxSize(params.getFileNames().size());
		FuiouUploadFileVO vo = null;
		FuiouFileUpload po = null;
		try {
			// 持久化文件
			vo = fuiouDepositFileInnerService.saveUpload(params, FuiouBusiTypeEnum.FILE_TRADE_UPLOAD);

			// 发送上传指令给文件服务
			po = fuiouDepositFileInnerService.doUpload(vo);

			OpResponse opResponse = new OpResponse();
			opResponse.setCode(po.getRspCode());
			opResponse.setDisplay(po.getRspMsg());
			return opResponse;
		} finally {
			// 释放内存
			vo = null;
			po = null;
		}
	}

	/**
	 * 下载文件
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:40:35
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/download")
	public OpResponse tradeDownload(@RequestBody @Valid DownloadFileReq params) {
		validBatchMaxSize(params.getFileNames().size());
		FuiouDownloadFileVO vo = null;
		FuiouFileDownload po = null;
		try {
			// 持久化文件
			vo = fuiouDepositFileInnerService.saveDownload(params, FuiouBusiTypeEnum.FILE_TRADE_DOWNLOAD);

			// 发送上传指令给文件服务
			po = fuiouDepositFileInnerService.doDownload(vo);

			OpResponse opResponse = new OpResponse();
			opResponse.setCode(po.getRspCode());
			opResponse.setDisplay(po.getRspMsg());
			return opResponse;
		} finally {
			vo = null;
			po = null;
		}
	}

	/**
	 * 交易回盘文件查询
	 * 
	 * @author buyi
	 * @date 2017-10-10 11:34:39
	 * @since v2.0.0
	 * @param params
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/trade/query")
	public OpResponse tradeQuery(@RequestBody @Valid FileQueryReq params) {
		validBatchMaxSize(params.getFileNames().size());

		// 保存订单信息
		FuiouFileQueryVO vo = fuiouDepositFileInnerService.saveQueryOrder(params, PayTypeEnum.FILE_TRADE);
		// 查询结果
		return fuiouDepositFileInnerService.query(vo);
	}

	/**
	 * 报备上传文件
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:40:45
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/upload")
	public OpResponse checkUpload(@RequestBody @Valid UploadFileReq params) {
		validBatchMaxSize(params.getFileNames().size());
		FuiouUploadFileVO vo = null;
		FuiouFileUpload po = null;
		try {
			// 持久化文件
			vo = fuiouDepositFileInnerService.saveUpload(params, FuiouBusiTypeEnum.FILE_CHECK_UPLOAD);

			// 发送上传指令给文件服务
			po = fuiouDepositFileInnerService.doUpload(vo);

			OpResponse opResponse = new OpResponse();
			opResponse.setCode(po.getRspCode());
			opResponse.setDisplay(po.getRspMsg());
			return opResponse;
		} finally {
			vo = null;
			po = null;
		}
	}

	/**
	 * 报备下载文件
	 * 
	 * @author buyi
	 * @date 2017-09-06 15:40:35
	 * @since v2.0.0
	 * @param req
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/download")
	public OpResponse checkDownload(@RequestBody @Valid DownloadFileReq params) {
		validBatchMaxSize(params.getFileNames().size());
		FuiouDownloadFileVO vo = null;
		FuiouFileDownload po = null;
		try {
			// 持久化文件
			vo = fuiouDepositFileInnerService.saveDownload(params, FuiouBusiTypeEnum.FILE_CHECK_DOWNLOAD);

			// 发送上传指令给文件服务
			po = fuiouDepositFileInnerService.doDownload(vo);

			OpResponse opResponse = new OpResponse();
			opResponse.setCode(po.getRspCode());
			opResponse.setDisplay(po.getRspMsg());
			return opResponse;
		} finally {
			vo = null;
			po = null;
		}
	}

	/**
	 * 报备回盘文件查询
	 * 
	 * @author buyi
	 * @date 2017-10-10 11:34:39
	 * @since v2.0.0
	 * @param params
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/check/query")
	public OpResponse checkQuery(@RequestBody @Valid FileQueryReq params) {
		validBatchMaxSize(params.getFileNames().size());

		// 保存订单信息
		FuiouFileQueryVO vo = fuiouDepositFileInnerService.saveQueryOrder(params, PayTypeEnum.FILE_CHECK);
		// 查询结果
		return fuiouDepositFileInnerService.query(vo);
	}

	/**
	 * 对账文件下载
	 * 
	 * @author buyi
	 * @date 2017-10-10 14:33:45
	 * @since v2.0.0
	 * @param params
	 * @return
	 */
	@HeaderSign
	@RequestMapping(method = RequestMethod.POST, path = "/api/fuiou/file/account/download")
	public OpResponse accountDownload(@RequestBody @Valid DownloadFileReq params) {
		validBatchMaxSize(params.getFileNames().size());
		FuiouDownloadFileVO vo = null;
		FuiouFileDownload po = null;
		try {
			// 持久化文件
			vo = fuiouDepositFileInnerService.saveDownload(params, FuiouBusiTypeEnum.FILE_ACCOUNT_DOWNLOAD);

			// 发送上传指令给文件服务
			po = fuiouDepositFileInnerService.doDownload(vo);

			OpResponse opResponse = new OpResponse();
			opResponse.setCode(po.getRspCode());
			opResponse.setDisplay(po.getRspMsg());
			return opResponse;
		} finally {
			vo = null;
			po = null;
		}
	}

	/**
	 * 校验上传最大size,如果超过限制，则抛出异常
	 * 
	 * @author buyi
	 * @date 2017-09-29 10:19:28
	 * @since v2.0.0
	 * @param size
	 */
	private void validBatchMaxSize(int size) {
		if (size > payConstants.getFileBatchMaxSize()) {
			throw PayExceptionCode.ERROR_ILL_PARAMETER.exp();
		}
	}
}
